Component({
  mixins: [],
  data: {
    likeArr:[],
    imgP:[]
  },
  props: {
    
    goods:"",          //商品图片
    likeBtn:"",        //未收藏的按钮
    likeBtnH:"",       //已收藏的按钮
    addCart:"",        //加购按钮
    likeBtnPosi:"",    //收藏按钮位置
    addCartBtnPosi:""  //加购按钮位置

  },

  onInit(){
    //this.imgSize();
    
  },

  didMount() {
    this.isCollect();
  },
  didUpdate() {},
  didUnmount() {},
  methods: {

    imgSize(e){
      let arr=this.data.imgP
      arr.push(e)
      //console.log(e);
      this.setData({
        imgP:arr
      })

      console.log(this.data.imgP)

    },

    // 是否收藏
    isCollect(){
      let newlikeA=[]
      for(let i in this.props.goods){
        newlikeA.push(0)
      }
      this.setData({likeArr:newlikeA})
      for(let i in this.props.goods){
        my.tb.checkGoodsCollectedStatus({
          id: this.props.goods[i].id,
          success: (res) => {
            res.isCollect && (newlikeA[i]=1)
            this.setData({likeArr:newlikeA})
          },
          fail: (res) => {
              
          }
        })
      }
    },

    //跳链接
    goLink(e){
      console.log(e.currentTarget.dataset.id);
      my.tb.openDetail ({
        itemId: e.currentTarget.dataset.id.toString(),
        success: (res) => {
          // my.alert({ content: "success" });
        },
        fail: (res) => {
          // my.alert({ content: "fail - " + res.error });
        },
      });

      // my.call('navigateToOutside',{ url:"https://detail.tmall.com/item.htm?spm=a1z10.3-b-s.w4011-16432970307.45.574c66f1U3W7OY&id=550080595188&rn=cead5389c1a0251da98a4a2c35aa9663&abbucket=14" })

    },



    //初始化就开始查询此处商品是否收藏过  有收藏就变按钮
    //先用接口查询是否收藏过
    //如果没有就收藏  按钮变样式

    //收藏
    like(e){
      my.tb.checkGoodsCollectedStatus({
        id: e.currentTarget.dataset.id,
        success: (res) => {
          if(res.isCollect){
            my.alert({ content: "已收藏" })
          }else{

            my.tb.collectGoods({
              id: e.currentTarget.dataset.id,
              success: (res) => {
                my.alert({ content: "收藏成功" });
                this.isCollect();

                // 此处做其他一些接后端的操作**************************

              },
              fail: (res) => {
                // my.alert({ content: "fail - " + JSON.stringify(res) })
              },
              complete: (res) => {
                // my.alert({ content: "complete - " + JSON.stringify(res) })
              }
            })

          }
          
        },
        fail: (res) => {
              
        }
      })


    },


    // 这里演示是需要申请的*******
    //加入购物车
    addCart(e){
      console.log(e.currentTarget.dataset.id)
      my.tb.addToCart({
        // itemIds: '574141925233', 不选sku和数量也能加购，需要用户在购物车中选好sku才能下单
        itemIds: e.currentTarget.dataset.id.toString(), // 对应itemId为574141925233 skuId为4018047819826 数量为4
        exts: 'text:text|123:456',
        success: (res) => {
          my.alert({ content: "success" + JSON.stringify(res) })
        },
        fail: (res) => {
          my.alert({ content: "fail" + JSON.stringify(res) })
          console.log(res)
        },
      })
    }


  },
});
